package com.diandiansong.app.ui.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import com.diandiansong.app.App;
import com.diandiansong.app.R;
import com.diandiansong.app.ui.base.BaseAct;
import com.diandiansong.app.utils.A;

import butterknife.BindView;
import butterknife.OnClick;
import diandiansong.library.utils.BaseEntity;
import diandiansong.library.utils.CCallBack;
import diandiansong.library.utils.TimeCount;

import static com.diandiansong.app.ui.user.RegisterStep1.isMobile;

/**
 * 修改密码
 */
public class ForgetPwd extends BaseAct {
    public static void start(Context context) {
        Intent starter = new Intent(context, ForgetPwd.class);
        context.startActivity(starter);
    }

    @BindView(R.id.et_phone)
    EditText mEtPhone;
    @BindView(R.id.et_code)
    EditText mEtCode;
    @BindView(R.id.do_get_code)
    TextView mDoGetCode;
    @BindView(R.id.et_pwd)
    EditText mEtPwd;
    @BindView(R.id.et_re_pwd)
    EditText mEtRePwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitle("修改密码");
        addContentView(R.layout.forget_pwd);
        if (App.DEBUG) {
            mEtPhone.setText("13212312311");
        }
    }

    @OnClick(R.id.do_get_code)
    public void onMDoGetCodeClicked() {
        String phone = mEtPhone.getText().toString();
        if (!isMobile(phone)) {
            toast("请输入您的手机号");
            return;
        }
        A.authFind_password_sms(phone, this, new CCallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity baseEntity) throws Exception {
                TimeCount.timerStart(60 * 1000, 1000, mDoGetCode, "重新获取", "s");
                toast("已发送验证码");
            }

            @Override
            public void onFailure(int code, String msg) {
                logger.e("onFailure() called with: " + "code = [" + code + "], msg = [" + msg + "]");
//                if (code == 500) {
                    TimeCount.timerStart(60 * 1000, 1000, mDoGetCode, "重新获取", "s");
                toast("已发送验证码");
//                } else {
//                    toast(msg);
//                }
            }
        });

    }

    @OnClick(R.id.do_next)
    public void onMDoNextClicked() {
        String phone = mEtPhone.getText().toString();
        if (!isMobile(phone)) {
            toast("请输入您的手机号");
            return;
        }
        String code = getText(mEtCode);
        if (code.isEmpty()) {
            toast("请输入验证码");
            return;
        }
        String pwd = getText(mEtPwd);
        String repwd = getText(mEtRePwd);
        if (pwd.length() < 6) {
            toast("密码太简单");
            return;
        }
        if (!pwd.equals(repwd)) {
            toast("两次输入的密码不一致");
            return;
        }
        A.authFind_password(phone, code, pwd, this, new CCallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity baseEntity) throws Exception {
                toast("修改密码成功");
                finish();
            }

            @Override
            public void onFailure(int code, String msg) {
                toast(msg);
            }
        });
    }


}
